import requests
import pandas as pd
import time

def get_expert_data(id, baseurl):
    url = baseurl + str(id)
    time.sleep(1.0)
    r = requests.get(url)

    data = r.json()
    age = data["data"]["age"]
    fans = data["data"]["fans"]
    articles = data["data"]["articles"]
    grade_name = data["data"]["gradeName"]
    dlt_one = data["data"]["dltOne"]
    dlt_two = data["data"]["dltTwo"]
    dlt_three = data["data"]["dltThree"]

    row = [id] + [age] + [fans] + [articles] + [grade_name] + [dlt_one] + [dlt_two] + [dlt_three]
    print(row)
    return row


def main():
    expert_data = []  # 存储专家数据
    baseurl = 'https://i.cmzj.net/expert/queryExpertById?expertId='

    df = pd.read_csv("csv/ids.csv")
    ids = df['id'].tolist()

    for id in ids:
        row = get_expert_data(id, baseurl)
        expert_data.append(row)

    columns = ['序号', '彩龄', '粉丝数', '文章数', '职位', '一等奖', '二等奖', '三等奖']
    data = pd.DataFrame(expert_data, columns=columns)
    data.to_csv("csv/expert_data.csv", index=False)


if __name__ == "__main__":
    main()